package vip.xiaonuo.issued.impl.member;

import cn.hutool.core.util.ObjectUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.facedoor.iservice.IMemberDeviceService;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.facedoor.modular.memberdevice.constant.MemberDeviceConstant;
import vip.xiaonuo.facedoor.modular.memberdevice.entity.MemberDevice;
import vip.xiaonuo.facedoor.modular.memberdevice.mapper.MemberDeviceMapper;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.memberuser.mapper.MemberUserMapper;
import vip.xiaonuo.facedoor.remote.iservice.adapter.MemberReqAdapter;

import javax.annotation.Resource;

import vip.xiaonuo.facedoor.rule.RuleConstant;

@Service(RuleConstant.DW + RuleConstant.MEMBER_DEVICESE)
public class MqttMemberDeviceServiceImpl implements IMemberDeviceService {

    @Resource
    private MemberUserMapper memberUserMapper;

    @Resource
    private MemberDeviceMapper memberDeviceMapper;

    @Resource
    private MemberReqAdapter personService;

    @Resource
    private DeviceBaseService deviceBaseService;


    private void baseCheck(MemberDevice memberDevice) {
        if (memberDevice == null) {
            throw new ServiceException(500, "找不到关联信息！");
        }
        if (memberDevice.getStatus().equals(MemberDeviceConstant.ADD) || memberDevice.getStatus().equals(MemberDeviceConstant.UPDATE) || memberDevice.getStatus().equals(MemberDeviceConstant.DELETE)) {
            throw new ServiceException(500, "非正常状态 不允许操作！");
        }
    }


    @Override
    public void delete(Long id) throws Exception {
        MemberDevice memberDevice = memberDeviceMapper.selectById(id);
        baseCheck(memberDevice);
        DeviceBase deviceBase = deviceBaseService.getById(memberDevice.getDeviceId());
        // 直接删除人员
        if (memberDevice.getStatus().equals(MemberDeviceConstant.ADD_FAIL)) {
            memberDeviceMapper.deleteById(id);
        } else {
            memberDevice.setStatus(MemberDeviceConstant.DELETE);
            memberDeviceMapper.updateById(memberDevice);
            personService.deletePerson(memberDevice, deviceBase.getId().toString());
        }
    }

    @Override
    public void sync(Long id) throws Exception {
        MemberDevice memberDevice = memberDeviceMapper.selectById(id);
        baseCheck(memberDevice);
        if (memberDevice.getStatus().equals(MemberDeviceConstant.SYNC_SUCCESS)) {
            throw new ServiceException(500, "未发生错误 不允许同步！");
        }
        DeviceBase deviceBase = deviceBaseService.getById(memberDevice.getDeviceId());
        MemberUser memberUser = memberUserMapper.selectById(memberDevice.getMemberId());
        // 恢复人员状态
        if (memberDevice.getStatus().equals(MemberDeviceConstant.ADD_FAIL)) {
            memberUser.setMemberOrVisitorDeviceId(memberDevice.getId());
            memberUser.setMemberType(1);
            // 存储柜管理员
            if (ObjectUtil.equal(memberDevice.getLockerAdmin(), Boolean.TRUE) && ObjectUtil.equal(deviceBase.getDeviceType(), "5")) {
                memberUser.setPersonType(3);
            }
            if (StringUtils.isNotBlank(memberDevice.getLockerData())){
                memberUser.setLockerData(memberDevice.getLockerData());
            }
            personService.addPerson(memberDevice.getDeviceId() + "", memberUser);
        }

        if (memberDevice.getStatus().equals(MemberDeviceConstant.UPDATE_FAIL)) {
            memberUser.setMemberOrVisitorDeviceId(memberDevice.getId());
            memberUser.setMemberType(1);
            // 存储柜管理员
            if (ObjectUtil.equal(memberDevice.getLockerAdmin(), Boolean.TRUE) && ObjectUtil.equal(deviceBase.getDeviceType(), "5")) {
                memberUser.setPersonType(3);
            }
            if (StringUtils.isNotBlank(memberDevice.getLockerData())){
                memberUser.setLockerData(memberDevice.getLockerData());
            }
            personService.updatePerson(memberDevice.getDeviceId() + "", memberUser);

        }
        if (memberDevice.getStatus().equals(MemberDeviceConstant.DELETE_FAIL)) {
            personService.deletePerson(memberDevice, deviceBase.getId().toString());
        }
        memberDeviceMapper.updateById(memberDevice);
    }
}
